package pl.plesken.swt;

import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import pl.plesken.languages.SetLanguage;
import pl.plesken.xml.CategoriesXML;

import com.swtdesigner.SWTResourceManager;

public class Categories {

	LinkedList<String> categories = new LinkedList<String>();
	private Table table;
	protected Shell shell;
	StringBuffer name = new StringBuffer();
	StringBuffer rename = new StringBuffer();
	StringBuffer status = new StringBuffer();

	public LinkedList<String> getCategories() {
		return categories;
	}

	public void setCategories(LinkedList<String> categories) {
		this.categories = categories;
	}

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Categories window = new Categories();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(Categories.class, "/icons/gui.png"));
		shell.setLayout(new GridLayout());
		shell.setSize(500, 316);
		shell.setText(SetLanguage.getString("Categories.AppName")); //$NON-NLS-1$

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout());

		final Group tableGroup = new Group(composite, SWT.NONE);
		final GridData gd_tableGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
		tableGroup.setLayoutData(gd_tableGroup);
		tableGroup.setLayout(new GridLayout());

		table = new Table(tableGroup, SWT.BORDER);
		table.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				CategoryConfig();
			}
		});
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final TableColumn nameTableColumn = new TableColumn(table, SWT.NONE);
		nameTableColumn.setWidth(120);
		nameTableColumn.setText(SetLanguage.getString("Categories.name")); //$NON-NLS-1$

		final TableColumn newNameTableColumn = new TableColumn(table, SWT.NONE);
		newNameTableColumn.setWidth(120);
		newNameTableColumn.setText(SetLanguage.getString("Categories.rename")); //$NON-NLS-1$

		final TableColumn statusTableColumn = new TableColumn(table, SWT.NONE);
		statusTableColumn.setWidth(120);
		statusTableColumn.setText(SetLanguage.getString("Categories.status")); //$NON-NLS-1$
		
		final TableColumn statTableColumn = new TableColumn(table, SWT.NONE);
		statTableColumn.setWidth(0);
		statTableColumn.setText("Stat"); //$NON-NLS-1$
		
		//wczytywanie
		//ConfigXML xml = new ConfigXML();
		//xml.parseCategoriesXML();
		CategoriesXML xml = new CategoriesXML();
		xml.getCategoryValues();
		
		for (int i = 0; i < xml.getRow().size(); i++) {
			TableItem it = new TableItem(table, SWT.NULL);
			String[] tab = xml.getRow().get(i).split(";"); //$NON-NLS-1$
			
			if (!tab[0].toString().equals("empty")) { //$NON-NLS-1$
				it.setText(0, tab[0].toString());
			} else {
				it.setText(0, ""); //$NON-NLS-1$
			}
			if (!tab[2].toString().equals("empty")) { //$NON-NLS-1$
				it.setText(1, tab[2].toString());
			} else {
				it.setText(1, ""); //$NON-NLS-1$
			}
			if (!tab[1].toString().equals("empty")) { //$NON-NLS-1$
				if (tab[1].toString().equals("true")) { //$NON-NLS-1$
					it.setText(2, SetLanguage.getString("Categories.enabled")); //$NON-NLS-1$
				} else if (tab[1].toString().equals("false")) { //$NON-NLS-1$
					it.setText(2, SetLanguage.getString("Categories.disabled")); //$NON-NLS-1$
				}
			} else {
				it.setText(2, ""); //$NON-NLS-1$
			}
			if (!tab[1].toString().equals("empty")) { //$NON-NLS-1$
				it.setText(3, tab[1].toString());
			} else {
				it.setText(3, "disable"); //$NON-NLS-1$
			}
			
		}

		final Group manageGroup = new Group(composite, SWT.NONE);
		manageGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		manageGroup.setLayout(gridLayout);

		final Button addButton = new Button(manageGroup, SWT.NONE);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				AddCategory();
			}
		});
		final GridData gd_addButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_addButton.widthHint = 80;
		addButton.setLayoutData(gd_addButton);
		addButton.setText(SetLanguage.getString("Categories.add")); //$NON-NLS-1$

		final Button delButton = new Button(manageGroup, SWT.NONE);
		delButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				DelCategory();
			}
		});
		
		final GridData gd_delButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_delButton.widthHint = 80;
		delButton.setLayoutData(gd_delButton);
		delButton.setText(SetLanguage.getString("Categories.delete")); //$NON-NLS-1$

		final Button setButton = new Button(manageGroup, SWT.NONE);
		setButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				save();
			}
		});
		final GridData gd_setButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_setButton.widthHint = 80;
		setButton.setLayoutData(gd_setButton);
		setButton.setText(SetLanguage.getString("Categories.set")); //$NON-NLS-1$

		final Button cancelButton = new Button(manageGroup, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_cancelButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_cancelButton.widthHint = 80;
		cancelButton.setLayoutData(gd_cancelButton);
		cancelButton.setText(SetLanguage.getString("Categories.cancel")); //$NON-NLS-1$
		//
	}

	private void save() {
		TableItem[] item = table.getItems();
		//ConfigXML xml = new ConfigXML();
		CategoriesXML xml = new CategoriesXML();
		
		for (int i = 0; i < item.length; i++) {
			name.setLength(0);
			rename.setLength(0);
			status.setLength(0);
			
			if (item[i].getText(0).equals("")) { //$NON-NLS-1$
				name.append("empty"); //$NON-NLS-1$
			} else {
				name.append(item[i].getText(0));
			}
			if (item[i].getText(1).equals("")) { //$NON-NLS-1$
				rename.append("empty"); //$NON-NLS-1$
			} else {
				rename.append(item[i].getText(1));
			}
			if (item[i].getText(3).equals("")) { //$NON-NLS-1$
				status.append("empty"); //$NON-NLS-1$
			} else {
				status.append(item[i].getText(3));
			}
			categories.add(i, name.toString() + ";" + rename.toString() + ";" + status.toString()); //$NON-NLS-1$ //$NON-NLS-2$
			//System.out.println(i + ": " + name.toString() + ";" + rename.toString() + ";" + status.toString());
		}
		xml.setSave(categories);
		//xml.saveCategoriesXML();
		xml.saveCategoryValues();
		//categories.clear();
		shell.close();
	}
	
	private void CategoryConfig() {
		CategoryConfig ct = new CategoryConfig(shell);
		
		TableItem[] item = table.getItems();
		
		ct.setName(item[table.getSelectionIndex()].getText(0));
		ct.setRename(item[table.getSelectionIndex()].getText(1));
		ct.setStatus(item[table.getSelectionIndex()].getText(3));
		
		ct.open();
		
		item[table.getSelectionIndex()].setText(0, ct.getName());
		item[table.getSelectionIndex()].setText(1, ct.getRename());
		if (ct.getStatus().equals("true")) { //$NON-NLS-1$
			item[table.getSelectionIndex()].setText(2, SetLanguage.getString("Categories.enabled")); //$NON-NLS-1$
		} else {
			item[table.getSelectionIndex()].setText(2, SetLanguage.getString("Categories.disabled")); //$NON-NLS-1$
		}
		item[table.getSelectionIndex()].setText(3, ct.getStatus());
	}
	
	private void AddCategory() {
		CategoryConfig ct = new CategoryConfig(shell);
		
		//ct.setName(SetLanguage.getString("Categories.origName")); //$NON-NLS-1$
		//ct.setRename(SetLanguage.getString("Categories.newName")); //$NON-NLS-1$
		ct.setName(""); //$NON-NLS-1$
		ct.setRename(""); //$NON-NLS-1$
		ct.setStatus(""); //$NON-NLS-1$
		
		ct.open();
		
		if (!ct.getName().equals("") && !ct.getRename().equals("")) {
			TableItem item = new TableItem(table, SWT.NULL);
			
			item.setText(0, ct.getName());
			item.setText(1, ct.getRename());
			if (ct.getStatus().equals("true")) { //$NON-NLS-1$
				item.setText(2, SetLanguage.getString("Categories.enabled")); //$NON-NLS-1$
			} else {
				item.setText(2, SetLanguage.getString("Categories.disabled")); //$NON-NLS-1$
			}
			item.setText(3, ct.getStatus());
		}
	}
	
	private void DelCategory() {
		table.remove(table.getSelectionIndices());
	}
	
}
